<template>
<div class="app-container">
<el-button type="warning" icon="el-icon-edit" @click="openChapterDialog">添加章节</el-button>
    <!-- 章节 -->
    <ul class="chapterList">
        <li v-for="chapter in chapterVideoList" 
        :key="chapter.id">
            <p>
                {{ chapter.title }}
                <span class="acts">
                    <el-button type="warning" round icon="el-icon-edit" @click="addVideoDialog(chapter.id)">添加小节</el-button>
                    <el-button type="text" style="color:green" icon="el-icon-edit" @click="openEditChapterDialog(chapter.id)">编辑</el-button>
                    <el-button type="text" style="color:red" icon="el-icon-delete" @click="deleteChapterById(chapter.id)">删除</el-button>
                </span>
            </p>
            <!-- 视频 -->
            <ul class="chapterList videoList">
                <li v-for="video in chapter.children" 
                :key="video.id">
                   <p>
                        {{ video.title }}
                        <span class="acts">
                        <el-tag v-if="!video.videoSourceId" size="mini" type="danger">{{ '尚未上传视频' }}</el-tag>
                        <el-tag v-if="video.isFree" size="mini" type="success">{{ '白嫖' }}</el-tag>
                            <el-button type="text" style="color:green" icon="el-icon-edit" @click="editVideo(chapter.id,video.id)">编辑</el-button>
                            <el-button type="text" style="color:red" icon="el-icon-delete" @click="removeVideo(video.id)">删除</el-button>
                        </span> 
                    </p>
                </li>
            </ul>
        </li>
    </ul>
    <!-- 章节表单对话框 -->
    <chapter-dialog ref="chapterDialog" />

     <!-- 小节表单对话框 -->
    <video-dialog ref="videoDialog" />

    <div style="text-align:center">
        <el-button  type="primary" @click="prev()">上一步</el-button>
        <el-button  type="success" @click="next()">下一步</el-button>
    </div>
</div>
</template>
<script>
import chapter from "@/api/teacher/chapter"
import video from "@/api/teacher/video"
import ChapterDialog from "@/views/course/components//chapter/form"
import VideoDialog from "@/views/course/components//video/form"
export default {
    components:{ ChapterDialog, VideoDialog },
    data() {
        return {
            chapterVideoList:[],
        }
    },
    created() {
        this.getChapterVideo()
    },
    methods: {
        //==========================小节操作=====================================
        addVideoDialog(chapterId){
            this.$refs.videoDialog.open(chapterId)
        },
        editVideo(chapterId,videoId){
            this.$refs.videoDialog.open(chapterId,videoId)
        },
        //删除小节
        removeVideo(videoId){
            this.$confirm('此操作将删除该小节，是否继续？','提示 ',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type:'warning'
            }).then(() => {
                    return video.deleteByVideoId(videoId)
                }
            ).then( res =>{
                    this.getChapterVideo()
                    this.$message.success(res.message)
            }).catch( res => {
                if(res === 'cancel'){
                     this.$message.info('取消删除')
                }
            })
        },
        //==========================章节操作=====================================
        openChapterDialog(){
            this.$refs.chapterDialog.open()
        },
        //删除章节
        deleteChapterById(chapterId){
            this.$confirm('此操作将删除章节，是否继续？','提示 ',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type:'warning'
            }).then(
                    () =>{
                    chapter.deleteByChapterId(chapterId)
                    .then(res =>{
                        this.$message({
                            type:'success',
                            message:'删除章节成功!'
                        })
                        this.getChapterVideo()
                    })
                }
            )
        },
        //修改章节弹框数据回显
        openEditChapterDialog(chapterId){
            this.$refs.chapterDialog.open(chapterId);
        },
         // 根据课程ID查询章节和小节
        getChapterVideo(){
            chapter.getAllChapterVideo(this.$parent.courseId)
            .then(res => {
                this.chapterVideoList = res.data.allChapterVideo
            })
        },
        prev(){
            this.$parent.active = 0
        },
        next() {
            this.$parent.active = 2
        }
    }
}
</script>

<style scoped>
.chapterList{
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
}
.chapterList li{
    position: relative;
}
.chapterList p{
    float: left;
    font-size: 20px;
    margin: 10px 0;
    padding: 10px;
    height: 70px;
    line-height: 50px;
    width: 100%;
    border: 1px solid #DDD;
  }

  .chapterList .acts {
    float: right;
    font-size: 14px;
  }

  .videoList {
    padding-left: 50px;
  }

  .videoList p {
    font-size: 14px;
    margin: 10px 0;
    padding: 10px;
    height: 50px;
    line-height: 30px;
    width: 100%;
    border: 1px dotted #DDD;
  }
</style>